package com.hycreate.platform.modules.crm.information;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.common.web.BaseController;
import com.hycreate.platform.modules.crm.project.ProjectService;
import com.hycreate.platform.modules.sys.entity.Office;
import com.hycreate.platform.modules.sys.entity.User;
import com.hycreate.platform.modules.sys.service.SystemService;
import com.hycreate.platform.modules.sys.utils.UserUtils;

@Controller
@RequestMapping(value="${adminPath}/information/information")
public class InformationController extends BaseController {
	/*
	 * 常量信息： 
	 * type = QUERY_BY_DAY 查当天数据
     * type = QUERY_BY_MONTH 查当月数据
     * type = QUERY_BY_YEAR 查当年数据
     * Map<key,value>
     *    key值取：
     *      “DEVELOP_NUM”：客户录入量
	 *		“TELE_NUM”：电话量
	 *		“PROJECT_FOLLOW”：项目跟进量
	 *		“PROJECT_VOLUME”：项目成交量
	 *	  Value:取值为具体数字
     *
	 */
	@Autowired
	InformationService service;
	@Autowired
	private SystemService systemService;
	@Autowired
	private ProjectService projectService;
	Map<String,Integer> map = new HashMap<String, Integer>();
	ObjectMapper mapper = new ObjectMapper();
	@ModelAttribute
	public User get(@RequestParam(required=false) String id) {
		User entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = systemService.getUser(id);
		}
		if (entity == null){
			entity = new User();
		}
		
		return entity;
	}
	
	//推进部经理查询今日、本月客户录入量和电话量
	@RequiresPermissions("information:index:view")
	@RequestMapping(value = {"index"})
	public String index(Model model,@RequestParam(value="type")String type) {
		
		User user = UserUtils.getUser();
		//根据部门ID 获取部门员工
		List<User>emp=projectService.findUser(user.getOffice().getId());
		model.addAttribute("emp",emp);
		//user 中获取员工id ，type 标识查一天的还是一个月的数据
		
		if(user.getOffice().getId()!=null){
			//officeId
		
			int deptDevelopNum = service.findDeptDevelop(user.getOffice().getId(), type);
			map.put("DEVELOP_NUM", deptDevelopNum);
			int deptTeleNum = service.findDeptTele(type);
			map.put("TELE_NUM", deptTeleNum);
		}
		model.addAttribute("map",map);
		return "modules/crm/information/advanceIndex";
	}
	//推进部经理查询今日、本月客户录入量和电话量(Ajax请求时)
	@RequestMapping(value = {"findAdvanceNum"})
	public void findAdvanceNum(HttpServletRequest request,HttpServletResponse response) throws IOException {
		
		//usre中有员工id ，type 表示查一天的还是一个月的数据
		//user中 office 来按部门查
		User user = UserUtils.getUser();
		StringBuilder result = new StringBuilder();
		
		String type = request.getParameter("type");
		
		String statusId = null;
		

		if(user.getOffice().getId()!=null){
			//officeId
		
			int deptDevelopNum = service.findDeptDevelop(user.getOffice().getId(), type);
			
			int deptTeleNum = service.findDeptTele(type);
			
			result.append("{")
			.append("\"developNum\":"+deptDevelopNum)
			.append(",")
			.append("\"teleNum\":"+deptTeleNum)			
			.append("}");
		}
		response.setContentType("test/javascript;charset=utf-8");
		
		response.getWriter().print(result.toString());
		
		
	}
	//推进部员工查询今日、本月客户录入量和电话量
	@RequiresPermissions("information:indexEmp:view")
	@RequestMapping(value = {"indexEmp"})
	public String indexEmp(Model model,@RequestParam(value="type")String type) {
		
		//user 中获取员工id ，type 标识查一天的还是一个月的数据
		User user = UserUtils.getUser();
		if(user.getId()!=null){
			//userId  
			int personDevelopNum = service.findPersonDevelop(user.getId(), type);
			map.put("DEVELOP_NUM", personDevelopNum);
			int personTeleNum = service.findPsersonTele(user.getId(), type);
			map.put("TELE_NUM",personTeleNum);
			
		}
		model.addAttribute("map",map);
		model.addAttribute("userId", user.getId());
		
		
		//测试git提交
		return "modules/crm/information/advanceEmpIndex";
	}
	
	//查询业务部今日、本月客户录入量和更进量、成交量
	@RequiresPermissions("information:form:view")
	@RequestMapping(value = {"form"})
	public String form(Model model,@RequestParam(value="type")String type) {
		
		//usre中有员工id ，type 表示查一天的还是一个月的数据
		//user中 office 来按部门查
		User user = UserUtils.getUser();
		//根据部门ID 获取部门员工
		List<User>emp=projectService.findUser(user.getOffice().getId());
		model.addAttribute("emp",emp);

		String statusId = null;
		
		if(user.getOffice().getId()!=null){
		
			int deptDevelopNum = service.findDeptDevelop(user.getOffice().getId(), type);
			map.put("DEVELOP_NUM", deptDevelopNum);
			statusId = service.findProjectStatusId("项目跟进中");
			int	deptProjectFollowNum = service.findDeptProject(user.getOffice().getId(),statusId, type);
			map.put("PROJECT_FOLLOW", deptProjectFollowNum);	
			statusId = service.findProjectStatusId("项目完成");
			int deptProjectVolumeNum = service.findDeptProject(user.getOffice().getId(),statusId, type);	
			map.put("PROJECT_VOLUME", deptProjectVolumeNum);	
			
		}
		model.addAttribute("map",map);
			
		return "modules/crm/information/businessForm";
	}
	//查询业务部今日、本月客户录入量和更进量、成交量(Ajax请求时)
	@RequestMapping(value = {"findBusinessNum"})
	public void findBusinessNum(HttpServletRequest request,HttpServletResponse response) throws IOException {
		
		//usre中有员工id ，type 表示查一天的还是一个月的数据
		//user中 office 来按部门查
		User user = UserUtils.getUser();
		StringBuilder result = new StringBuilder();
		
		String type = request.getParameter("type");
		
		String statusId = null;
		
		if(user.getOffice().getId()!=null){
			
			int deptDevelopNum = service.findDeptDevelop(user.getOffice().getId(), type);
		
			statusId = service.findProjectStatusId("项目跟进中");
			int	deptProjectFollowNum = service.findDeptProject(user.getOffice().getId(),statusId, type);
				
			statusId = service.findProjectStatusId("项目完成");
			int deptProjectVolumeNum = service.findDeptProject(user.getOffice().getId(),statusId, type);	
				
			
			result.append("{")
			.append("\"developNum\":"+deptDevelopNum)
			.append(",")
			.append("\"projectFollow\":"+deptProjectFollowNum)
			.append(",")
			.append("\"projectVolume\":"+deptProjectVolumeNum)
			.append("}");
		}
		response.setContentType("text/json;charset=utf-8");
		
		response.getWriter().print(result.toString());
		
		
	}
	//查询业务部员工今日、本月客户录入量和更进量、成交量
	@RequiresPermissions("information:formEmp:view")
	@RequestMapping(value = {"formEmp"})
	public String formEmp(Model model,@RequestParam(value="type")String type) {
		
		//usre中有员工id ，type 表示查一天的还是一个月的数据
		User user = UserUtils.getUser();
		String statusId = null;
		
		if(user.getId()!=null&&user.getOffice().getId()!=null){
			
			int personDevelopNum = service.findPersonDevelop(user.getId(), type);
			map.put("DEVELOP_NUM", personDevelopNum);
			
			statusId = service.findProjectStatusId("项目跟进中");
			int	personProjectFollowNum = service.findPersonProject(user.getId(), statusId, type);
			map.put("PROJECT_FOLLOW", personProjectFollowNum);
			
			statusId = service.findProjectStatusId("项目完成");
			int personProjectVolumeNum = service.findPersonProject(user.getId(), statusId, type);
			map.put("PROJECT_VOLUME", personProjectVolumeNum);
				
		}
		model.addAttribute("userId", user.getId());
		model.addAttribute("map",map);
		
		return "modules/crm/information/businessEmpForm";
	}
		
	//查询全部
	@RequiresPermissions("information:list:view")
	@RequestMapping(value = {"list",""})
	public String list(Model model,@RequestParam(value="type")String type) {
		//user中获取userId查指定员工的数据
		//user不指定具体的userId；查公司所有的 日报表
		User user = UserUtils.getUser();
		//查询所有的部门
		List<Office> dept = projectService.findAllDept();
		model.addAttribute("dept",dept);
		
		int GeneralDevelopNum = service.findGeneralDevelop(type);
		map.put("DEVELOP_NUM", GeneralDevelopNum);
		
		int GeneralTeleNum = service.findDeptTele(type);
		map.put("TELE_NUM", GeneralTeleNum);
		
		String statusId = service.findProjectStatusId("项目跟进中");
		int GeneralProjectFollowNum = service.findDeptProject(user.getOffice().getId(),statusId, type);
		map.put("PROJECT_FOLLOW", GeneralProjectFollowNum);
		
		statusId = service.findProjectStatusId("项目完成");
		int GeneralProjectVolumeNum = service.findDeptProject(user.getOffice().getId(),statusId, type);
		map.put("PROJECT_VOLUME",GeneralProjectVolumeNum);
			
		model.addAttribute("map",map);
		
		return "modules/crm/information/informationList";
		
		
	}
	
	//总经理/经理 查看 个人的 工作量
	@RequestMapping(value = "findPersonNum")
	public void findPersonNum(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="type")String type) throws IOException {
		//默认查一天数据
//		String type = "QUERY_BY_DAY";
		User user = UserUtils.getUser();
		StringBuilder result = new StringBuilder();
		String id = request.getParameter("userId");
		if(id==null){
			int GeneralDevelopNum = service.findGeneralDevelop(type);
			
			
			int GeneralTeleNum = service.findDeptTele(type);
		
			
			String statusId = service.findProjectStatusId("项目跟进中");
			int GeneralProjectFollowNum = service.findDeptProject(user.getOffice().getId(),statusId, type);
			//System.out.println("=============是我进来了================");
			
			statusId = service.findProjectStatusId("项目完成");
			int GeneralProjectVolumeNum = service.findDeptProject(user.getOffice().getId(),statusId, type);
			result.append("{")
			  .append("\"developNum\":"+GeneralDevelopNum)
			  .append(",")
			  .append("\"teleNum\":"+GeneralTeleNum)
			  .append(",")
			  .append("\"projectFollow\":"+GeneralProjectFollowNum)
			  .append(",")
			  .append("\"projectVolume\":"+GeneralProjectVolumeNum)
			  .append("}");
		}else{
			
			//User user = UserUtils.getUser();
			int personDevelopNum = service.findPersonDevelop(id, type);
			
			int personTeleNum = service.findPsersonTele(id, type);
			
			String statusId = service.findProjectStatusId("项目跟进中");
			int	personProjectFollowNum = service.findPersonProject(id, statusId, type);
			
			statusId = service.findProjectStatusId("项目完成");
			int personProjectVolumeNum = service.findPersonProject(id, statusId, type);
			result.append("{")
			.append("\"developNum\":"+personDevelopNum)
			.append(",")
			.append("\"teleNum\":"+personTeleNum)
			.append(",")
			.append("\"projectFollow\":"+personProjectFollowNum)
			.append(",")
			.append("\"projectVolume\":"+personProjectVolumeNum)
			.append("}");
		}
//		String numJson = 
//				"{'developNum':"+personDevelopNum+",'teleNum':"+personTeleNum+
//				",'projectFollow':"+personProjectFollowNum+",'projectVolume':"+personProjectVolumeNum+" }";
	    
		response.setContentType("test/javascript;charset=utf-8");
		//response.setContentType("Content-type: text/html;charset=utf-8");
		response.getWriter().print(result.toString());
	 
	}
	
//	public Map<String,Integer> queryAdvance(User user,String type){
//		//user 中获取员工id ，type 标识查一天的还是一个月的数据
//		if(user.getId()!=null){
//			
//			int personDevelopNum = service.findPersonDevelop(user, type);
//			map.put("DEVELOP_NUM", personDevelopNum);
//			int personTeleNum = service.findPsersonTele(user, type);
//			map.put("TELE_NUM",personTeleNum);
//			
//			
//		}
//	    return map;
//	}
//	 
//	 
//	//业务部或员工查询今日、本月客户录入量和更进量、成交量	
//	//@RequestMapping(value = {"form"})	
//	public Map<String,Integer> queryBusiness(User user,String type){
//		//usre中有员工id ，type 表示查一天的还是一个月的数据
//		
//		String statusId = null;
//		
//		if(user.getId()!=null){
//			int personDevelopNum = service.findPersonDevelop(user, type);
//			map.put("DEVELOP_NUM", personDevelopNum);
//			
//			statusId = service.findProjectStatusId("项目跟进中");
//			int	personProjectFollowNum = service.findPersonProject(user, statusId, type);
//			map.put("PROJECT_FOLLOW", personProjectFollowNum);
//			
//			statusId = service.findProjectStatusId("项目完成");
//			int personProjectVolumeNum = service.findPersonProject(user, statusId, type);
//			map.put("PROJECT_VOLUME", personProjectVolumeNum);	
//		}
//	
//		return map;
//	}
	//查询全部
	/*
	 * 
	 *
	@RequestMapping(value = {"form"})
	public Map<String,Integer> queryGeneral(@RequestParam(value="user",required=false)User user,
			@RequestParam(value="type")String type){
		//user中获取userId查指定员工的数据
		//user不指定具体的userId；查公司所有的 日报表
		if(user.getId()!=null){
			if(user.getOffice().getCode()=="推进部"){
				user.getOffice().setCode("二狗你变了");
				map = this.queryAdvance(user, type);				
			}
			if(user.getOffice().getCode()=="业务部"){
				user.getOffice().setCode("二狗你变了");
				map =  this.queryBusiness(user, type);
			}
			
		}else{
			int GeneralDevelopNum = service.findGeneralDevelop(type);
			map.put("DEVELOP_NUM", GeneralDevelopNum);
			int GeneralTeleNum = service.findDeptTele(type);
			map.put("TELE_NUM", GeneralTeleNum);
			int statusId = service.findProjectStatusId("项目跟进中");
			int GeneralProjectFollowNum = service.findDeptProject(statusId, type);
			map.put("PROJECT_FOLLOW", GeneralProjectFollowNum);
			statusId = service.findProjectStatusId("项目完成");
			int GeneralProjectVolumeNum = service.findDeptProject(statusId, type);
			map.put("PROJECT_VOLUME",GeneralProjectVolumeNum);
			
		}
		return map;
	}
	//推进部查询今日、本月客户录入量和电话量
	//@RequiresPermissions("information:information:view")
	//@RequestMapping(value = {""})
//	public String queryAdvanceController(User user,String type){
//		map = service.queryAdvance(user, type);
//		return "";
//	}
	
	//业务部或员工查询今日、本月客户录入量和更进量、成交量
//	@RequiresPermissions("information:information:view")
//	@RequestMapping(value = {""})
//	public String queryBusinessController(User user,String type){
//		map = service.queryBusiness(user, type);
//		
//		return "";
//	}
//	@RequiresPermissions("information:information:view")
//	@RequestMapping(value = {""})
//	public String queryGeneralController(User user,String type){
//		
//		map = service.queryGeneral(user, type);
//		return "";
//	}
	*/
}